﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace StockMentor
{
    public partial class frmEditProduse : Form
    {
        private int iIdProd;
        DataSet dsProduse = new DataSet();
        DataTable dtProduse = new DataTable();
        SqlDataAdapter daProduse = new SqlDataAdapter();
        string sProd = string.Empty;
        SqlCommand commProd = new SqlCommand();
        public static SqlConnection connStatic;
        SqlConnection conn;
        private static Form _singletonInstance = null;
        private Form formCalling;
     
        public frmEditProduse(SqlConnection conn)
        {
            InitializeComponent();
            this.conn = conn;

        }

        public void SetKeyValues(int iIdProd, Form formCalling)
        {
            this.iIdProd = iIdProd;
            this.formCalling = formCalling;
        }

        private void btnIesire_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public static Form GetSingletonInstance(SqlConnection conection)
        {
            if (_singletonInstance == null || _singletonInstance.IsDisposed)
            {
                connStatic = conection;
                _singletonInstance = new frmEditProduse(connStatic);
            }
            return _singletonInstance;
        }

        private void frmEditProduse_Load(object sender, EventArgs e)
        {
            sProd = "SELECT * FROM produse WHERE prod_id = " + iIdProd.ToString();

            daProduse = new SqlDataAdapter(sProd, conn);
            dsProduse = new DataSet();

            daProduse.Fill(dsProduse, "produse");

            dtProduse = dsProduse.Tables[0];

            txtDenumire.Text = dtProduse.Rows[0]["prod_denumire"].ToString();
            txtCod.Text = dtProduse.Rows[0]["prod_cod"].ToString();
            txtUm.Text = dtProduse.Rows[0]["prod_um"].ToString();
            comboCota.SelectedIndex = comboCota.FindString(dtProduse.Rows[0]["prod_cota_tva"].ToString());
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            switch (MessageBox.Show("Doriti sa efectuati salvarea produsului cu noile date?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                case DialogResult.Yes:
                    break;
                case DialogResult.No:
                    return;
                    break;
            }
            try
            {
                string sMsg = string.Empty;

                if (txtDenumire.Text.Trim().Length == 0)
                {
                    sMsg = " Denumire ";
                    goto msgErr;
                }
                if (txtUm.Text.Trim().Length == 0)
                {
                    sMsg = " UM ";
                    goto msgErr;
                }
                if (comboCota.SelectedIndex == -1)
                {
                    sMsg = " Cota TVA ";
                    goto msgErr;
                }


                CustomProcedures.conn = connStatic;
                CustomProcedures.UPDATEPRODUSE(iIdProd ,
                                                 txtDenumire.Text,
                                                 txtCod.Text,
                                                 txtUm.Text,
                                                 Convert.ToInt32 (comboCota.Text )
                                                 );
                MessageBox.Show("Salvarea produsului s-a efectuat cu succes!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

                ((frmProduse)formCalling).CallBack();
                this.Close();
                return;
            msgErr: MessageBox.Show("Pentru adaugarea unui produs, campul" + sMsg + "trebuie completat!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Datele despre produs nu au fost salvate. S-au inregistrat erori!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnSterge_Click(object sender, EventArgs e)
        {
            switch (MessageBox.Show("Doriti sa efectuati stergerea produsului?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            { 
                case DialogResult.Yes:
                    break;
                case DialogResult.No:
                    return;
                    break;
            }
           
            try
            {
                sProd = "DELETE FROM produse WHERE prod_id = " + iIdProd.ToString();

                commProd = new SqlCommand(sProd, conn);


                SqlDataReader readerFurn = commProd.ExecuteReader();

                readerFurn.Dispose();
                readerFurn.Close();
                txtDenumire.Text = txtCod.Text = txtUm.Text = string.Empty;
                comboCota.SelectedIndex = -1;

                MessageBox.Show("Stergerea produsului s-a efectuat cu succes!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ((frmProduse)formCalling).CallBack();
                
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Stergerea produsului nu s-a efectuat. S-au intampinat erori la stergere!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
    }
}
